package com.icinema.main;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.icinema.client.Message;
import com.icinema.client.MovieService;
import com.icinema.client.MovieServiceAsync;
import com.icinema.client.RatingService;
import com.icinema.client.RatingServiceAsync;
import com.icinema.vo.MovieDetail;
import com.icinema.vo.Rating;

public class AddMovieView extends Composite{
	ListBox ratingList = new ListBox();
	
	TextBox txtMovieName = new TextBox();
	private final RatingServiceAsync ratingService = GWT
	.create(RatingService.class);
	
	private final MovieServiceAsync movieService = GWT
	.create(MovieService.class);
	
	
	FlexTable movieTable = new FlexTable();
	public AddMovieView(){
		VerticalPanel panel = new VerticalPanel();
		Grid grid = new Grid(3,2);
		initWidget(panel);
		grid.setWidget(0,0,new Label("Movie Name"));
		grid.setWidget(0,1,txtMovieName);
		grid.setWidget(1,0,new Label("Rating"));
		grid.setWidget(1,1,ratingList);
		Button buttonOK = new Button("OK");
		grid.setWidget(2,1,buttonOK);
		txtMovieName.setStyleName("textBox");
		CellFormatter formatter = grid.getCellFormatter();
		formatter.setStyleName(2,1,"createRatingOkButton");
		buttonOK.addMouseUpHandler(new MouseUpHandler(){
			
			@Override
			public void onMouseUp(MouseUpEvent event) {
				movieService.addMovie(txtMovieName.getText(),ratingList.getValue(ratingList.getSelectedIndex()),
						new AsyncCallback<String>(){

							@Override
							public void onFailure(Throwable caught) {
							}

							@Override
							public void onSuccess(String result) {
								txtMovieName.setText("");
								refresh();
							}
						}
					);
			}
			
		});
		panel.add(grid);
		panel.add(movieTable);
	}
	
	public void refresh(){
		this.setVisible(false);
		this.setVisible(true);
	}	

	class ImageDeleteMouseDownHandler implements  MouseDownHandler{
		private String movieShowingId;
		Message msg = new Message();
		public ImageDeleteMouseDownHandler(String movieShowingId){
			this.movieShowingId = movieShowingId;
		}
		@Override
		public void onMouseDown(MouseDownEvent event) {
			if(!Window.confirm("This will Delete the Movie Information")){
				return;
			}
			
			movieService.deleteMovie(movieShowingId, new AsyncCallback<Message>(){

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(Message result) {
					if(!result.getErr_msg().equals("")){
						Window.alert(result.getErr_msg());
					}
				}
			});
			refresh();
		}
	}	
	
	@Override
	public void setVisible(boolean visible) {
			if(visible == true){
				ratingList.clear();
				movieTable.removeAllRows();
				movieTable.getRowFormatter().setStyleName(0,"tableViewBrowseHeaders");
				movieTable.setText(0,0,"Movie Name");
				movieTable.setText(0,1,"Rating");

				ratingService.getRatings(new AsyncCallback<List<Rating>>(){
					@Override
					public void onFailure(Throwable caught) {
					}
		
					@Override
					public void onSuccess(List<Rating> results) {
						for(Rating rating:results){
							ratingList.addItem(rating.getName(),rating.getId());
						}
					}
				});
				
				movieService.getMovies(new AsyncCallback<List<MovieDetail>>(){
					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(List<MovieDetail> result) {
						for(int i=0;i<result.size();i++){
							Image imgDelete = new Image("/images/delete.png");
							imgDelete.setStyleName("imgButton");
							String showId = result.get(i).getMovie().getId();
							imgDelete.addMouseDownHandler(new ImageDeleteMouseDownHandler(showId));
							movieTable.setText(i+1, 0,result.get(i).getMovie().getName());
							movieTable.setText(i+1, 1,result.get(i).getRating().getName());
							movieTable.setWidget(i+1,2,imgDelete);
							movieTable.getCellFormatter().setStyleName(i+1, 0, "tableCell");
							movieTable.getCellFormatter().setStyleName(i+1, 1, "tableCell");
						}
					}
				});
			}
		super.setVisible(visible);
	}

}
